package com.fjh.springboot_01.controller;

import com.fjh.springboot_01.pojo.homework.KnowledgePoint;
import com.fjh.springboot_01.service.KnowledgePointService;
import com.fjh.springboot_01.pojo.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/api/knowledge-points")
public class KnowledgePointController {
    @Autowired
    private KnowledgePointService knowledgePointService;

    @PostMapping
    public Result<Void> addKnowledgePoint(@RequestBody KnowledgePoint knowledgePoint) {
        try {
            knowledgePointService.addKnowledgePoint(knowledgePoint);
            return Result.success();
        } catch (Exception e) {
            return Result.error("添加知识点失败：" + e.getMessage());
        }
    }

    @GetMapping
    public Result<List<KnowledgePoint>> getKnowledgePoints() {
        try {
            List<KnowledgePoint> points = knowledgePointService.getKnowledgePoints();
            return Result.success(points);
        } catch (Exception e) {
            return Result.error("获取知识点列表失败：" + e.getMessage());
        }
    }

    @GetMapping("/category/{categoryId}")
    public Result<List<KnowledgePoint>> getKnowledgePointsByCategory(@PathVariable Integer categoryId) {
        try {
            List<KnowledgePoint> points = knowledgePointService.getKnowledgePointsByCategory(categoryId);
            return Result.success(points);
        } catch (Exception e) {
            return Result.error("获取分类知识点列表失败：" + e.getMessage());
        }
    }

    @PutMapping("/{id}")
    public Result<Void> updateKnowledgePoint(
            @PathVariable Integer id,
            @RequestBody KnowledgePoint knowledgePoint
    ) {
        try {
            knowledgePoint.setId(id);
            knowledgePointService.updateKnowledgePoint(knowledgePoint);
            return Result.success();
        } catch (Exception e) {
            return Result.error("更新知识点失败：" + e.getMessage());
        }
    }

    @DeleteMapping("/{id}")
    public Result<Void> deleteKnowledgePoint(@PathVariable Integer id) {
        try {
            knowledgePointService.deleteKnowledgePoint(id);
            return Result.success();
        } catch (Exception e) {
            return Result.error("删除知识点失败：" + e.getMessage());
        }
    }

    @PostMapping("/homework/{homeworkId}")
    public Result<Void> addHomeworkKnowledge(
            @PathVariable Integer homeworkId,
            @RequestBody List<Integer> knowledgeIds
    ) {
        try {
            knowledgePointService.addHomeworkKnowledge(homeworkId, knowledgeIds);
            return Result.success();
        } catch (Exception e) {
            return Result.error("设置作业知识点失败：" + e.getMessage());
        }
    }

    @GetMapping("/homework/{homeworkId}")
    public Result<List<KnowledgePoint>> getHomeworkKnowledge(@PathVariable Integer homeworkId) {
        try {
            List<KnowledgePoint> points = knowledgePointService.getHomeworkKnowledge(homeworkId);
            return Result.success(points);
        } catch (Exception e) {
            return Result.error("获取作业知识点失败：" + e.getMessage());
        }
    }
} 